package com.hmjk.health.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.format.Time;

public class LocationTimeScanner {
	private JSONObject[] data;
	private ArrayList<Parsed> parsed;
	private int next = 0;;
	
	class Parsed{
		long time;
		String data;
		
		public Parsed(long t, String s){
			time = t;
			data = s;
		}
	}
	
	public LocationTimeScanner(JSONArray array) throws JSONException{
		initData(array);
	}
	
	public void resetData(JSONArray array) throws JSONException{
		data = null;
		parsed = null;
		next = 0;
		initData(array);
	}
	
	private void initData(JSONArray array) throws JSONException{
		sort(array);
		
		if((data == null) || (data.length == 0))
			return;
		
		parsed = new ArrayList<Parsed>();
		JSONArray one = new JSONArray();
		long time = 0;
		for(JSONObject e:data){
			long t = e.getLong("time");
			if(time == 0){
				time = t;
				one.put(e);
			}else if(isSameDay(time,t)){
				one.put(e);
			}else{
				if(one.length() > 0){
					parsed.add(new Parsed(time, one.toString()));
					one = new JSONArray();
				}
				time = t;
				one.put(e);
			}
		}
		if(one.length() > 0){
			parsed.add(new Parsed(time, one.toString()));
		}
	}
	
	private boolean isSameDay(long t1, long t2){
		Time time = new Time();
		time.setToNow();
		time.set(t1);
		int t1year = time.year;
		int t1yearday = time.yearDay;
		
		time.set(t2);
		
		if((t1year == time.year) && (t1yearday == time.yearDay)){
			return true;
		}
		
		return false;
	}
	
	public ArrayList<JSONObject> getSortedArray(){
		if((data == null) || (data.length == 0))
			return null;
		
		ArrayList<JSONObject> ret = new ArrayList<JSONObject>();
		for(JSONObject e:data){
			ret.add(e);
		}
		return ret;
	}
	
	private void sort(JSONArray array) throws JSONException{
		int size = array.length();
		ArrayList<JSONObject> list = new ArrayList<JSONObject>();
		for(int i= 0; i < size; i++){
			JSONObject e = array.getJSONObject(i);
			if(e.has("time")){
				list.add(e);
			}
		}
		
		size = list.size();
		if(size > 0){
			data = new JSONObject[size];
			data = list.toArray(data);
			
			/*Arrays.sort(data,0,size,new Comparator<JSONObject>(){

				@Override
				public int compare(JSONObject arg0, JSONObject arg1) {
					int flags = 0;
					try {
						long t0 = arg0.getLong("time");
						long t1 = arg1.getLong("time");
						if (t0 < t1) {
							flags = 1;
						}else if(t0 > t1) {
							flags = -1;
						}else{
							flags = 0;
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					return flags;
				}
				
			});*/
		
		}
	}
	
	public boolean hasNext(){
		if(parsed == null)
			return false;
		
		int total = parsed.size();
		if(total <= 0){
			return false;
		}
		
		if(next < total)
			return true;
		
		return false;
	}
	
	public String checkNextDateString(){
		Parsed p = parsed.get(next);
		long longtime = p.time;
		Time time = new Time();
		time.setToNow();
		int year = time.year;
		int yearday = time.yearDay;
		//int mon = time.month;
		//int day = time.monthDay;
		//int wday = time.weekDay;
		time.set(longtime);
		
		String datestr;
		
		if(time.year == year && time.yearDay == yearday){
			datestr = "today";
		}else if(time.year == year && time.yearDay == (yearday-1)){
			datestr = "yesterday";
		}else{
			datestr = String.format("%04d-%02d-%02d", time.year,time.month+1,
				time.monthDay);
		}
		
		return datestr;
	}
	
	public JSONArray get() throws JSONException{
		Parsed p = parsed.get(next);
		JSONArray ret = new JSONArray(p.data);
		next++;
		return ret;
	}
}
